﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AutoUpdateService.Mgr.Handler
{
    using System.IO;
    using System.Linq;
    using AutoUpdate.Model.Packet;
    public class createVersionHandler : Handler
    {
        public createVersionHandler()
        {
            this.name = "createVersion";
        }

        public override void doAction(Packet packet)
        {
            string versionPath = Environment.CurrentDirectory + "\\" + ConfigMgr.getInstance().config.versionPath;

            string versionFile = Environment.CurrentDirectory + "\\" + ConfigMgr.getInstance().config.versionPath + "\\version.ini";

            List<AutoUpdate.Model.Model.fileTransfer.fs_version> versionList = null;

            if (!Directory.Exists(versionPath))
            {
                Directory.CreateDirectory(versionPath);
            }

            if (!File.Exists(versionFile))
            {
                //File.WriteAllText(versionFile, packet.MessageContent, Encoding.UTF8);

                AutoUpdate.Model.Model.fileTransfer.fs_version versionModel = AutoUpdate.Utility.Helper.JsonHelper.getObject<AutoUpdate.Model.Model.fileTransfer.fs_version>(packet.MessageContent);

                if (versionModel != null)
                {
                    versionList = new List<AutoUpdate.Model.Model.fileTransfer.fs_version>();

                    versionList.Add(versionModel);

                    File.WriteAllText(versionFile, AutoUpdate.Utility.Helper.JsonHelper.getString(versionList), Encoding.UTF8);

                    if (versionList.Where(a => a.version == versionModel.version).ToList().Count == 0)
                    {

                        versionList.Add(versionModel);

                        File.WriteAllText(versionFile, AutoUpdate.Utility.Helper.JsonHelper.getString(versionList), Encoding.UTF8);

                        if (!Directory.Exists(versionPath + "\\" + "ver" + versionModel.version))
                        {
                            Directory.CreateDirectory(versionPath + "\\" + "ver" + versionModel.version);
                        }


                        AutoUpdate.Model.Packet.ResponsePacket response = new ResponsePacket();

                        packet.FillData(response);

                        response.PacketType = PacketType.Response;

                        response.MessageContent = Newtonsoft.Json.JsonConvert.SerializeObject(versionList);

                        ClientMgr.getInstance().transferMessage(response);
                    }
                }
            }
            else
            {
                string value = File.ReadAllText(versionFile, Encoding.UTF8);

                if (!string.IsNullOrEmpty(value))
                {
                    versionList = AutoUpdate.Utility.Helper.JsonHelper.getObject<List<AutoUpdate.Model.Model.fileTransfer.fs_version>>(value);
                }

                AutoUpdate.Model.Model.fileTransfer.fs_version versionModel = AutoUpdate.Utility.Helper.JsonHelper.getObject<AutoUpdate.Model.Model.fileTransfer.fs_version>(packet.MessageContent);

                if (versionModel != null)
                {
                    if (versionList.Where(a => a.version == versionModel.version).ToList().Count == 0)
                    {

                        versionList.Add(versionModel);

                        File.WriteAllText(versionFile, AutoUpdate.Utility.Helper.JsonHelper.getString(versionList), Encoding.UTF8);

                        if (!Directory.Exists(versionPath + "\\" + "ver" + versionModel.version))
                        {
                            Directory.CreateDirectory(versionPath + "\\" + "ver" + versionModel.version);
                        }


                        AutoUpdate.Model.Packet.ResponsePacket response = new ResponsePacket();

                        packet.FillData(response);

                        response.PacketType = PacketType.Response;

                        response.MessageContent = Newtonsoft.Json.JsonConvert.SerializeObject(versionList);

                        ClientMgr.getInstance().transferMessage(response);
                    }
                }
            }
        }
    }
}
